/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.magostini.mailservice.sender;

import java.util.HashMap;
import java.util.Map;
import javax.mail.internet.MimeMessage;
import org.apache.log4j.Logger;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.ui.velocity.VelocityEngineUtils;

/**
 *
 * @author marco
 * 
 */

public class CommentSender implements Runnable{
     Logger log =Logger.getLogger("INTEGRATION-MAILSERVICE");
   


    private JavaMailSender mailSender;
    private String emailWisher;
    private String userNameWisher;
    private String userNameComment;
    private String idWish;
    private String titleWish;
    private String textWish;
    private String commentWish;

    private VelocityEngine velocityEngine;
    private String url;

    public CommentSender(JavaMailSender mailSender, String emailWisher, String userNameWisher, String userNameComment,
            String idWish,String titleWish, String textWish, String commentWish, VelocityEngine velocityEngine, String url) {
        this.mailSender = mailSender;
        this.emailWisher = emailWisher;
        this.userNameWisher = userNameWisher;
        this.userNameComment = userNameComment;
        this.idWish = idWish;
        this.titleWish = titleWish;
        this.textWish = textWish;
        this.commentWish = commentWish;
        this.velocityEngine = velocityEngine;
        this.url = url;
    }

    

    @Override
    public void run() {

        
        MimeMessagePreparator preparator = new MimeMessagePreparator() {

                    @Override
                    public void prepare(MimeMessage mimeMessage) throws Exception {
        
                        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
                        message.setTo(emailWisher);
                        message.setSubject("WaveWish - Commento da "+userNameComment+((titleWish!=null)?" per "+titleWish:""));
                        message.setFrom("WaveWish@wavewish.it"); // could be parameterized...
                        
                        Map model = new HashMap();
                        
                        model.put("username", userNameWisher);
                        model.put("usernameComment", userNameComment);
                        model.put("idWish", idWish);
                        model.put("titleWish", titleWish);
                        model.put("textWish", textWish);
                        model.put("link", url);
                        model.put("islinkPresent", (url!=null));

                        String text = VelocityEngineUtils.mergeTemplateIntoString(
                                velocityEngine, "template/comment.vm", model);
                        log.debug("set Text ");
                        message.setText(text, true);

//                        log.debug("add image ");
//                        message.addInline("imageHeader", new ClassPathResource("/image/Header-mail.jpg"));


                    }
                };
                log.info("sendig email ");

                try{
                        mailSender.send(preparator);
                }catch(Exception e){
                    log.error("NON SENDING EMAIL FOR COMMENT TO "+emailWisher,e);

                    return;
                }
                log.info("EMAIL SEND FOR COMMENT TO "+emailWisher);
    }

}
